package org.brisling.docManage.queryService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.brisling.authorize.shiro.ShiroSecurityService;
import org.brisling.base.domain.ListDomain;
import org.brisling.base.service.AbstractBaseQueryService;
import org.brisling.common.ConstSysParam;
import org.brisling.common.SysConfig;
import org.brisling.common.exception.DangerousOperatorException;
import org.brisling.common.util.JsonUtil;
import org.brisling.common.util.MapUtil;
import org.brisling.docManage.domain.Doctype;
import org.brisling.mobile.vo.document.VODoctype;
import org.brisling.websocket.domain.WSView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * 文档类型查询服務service
 * @Author : jackson wang
 * @Date   : 2015年6月8日 下午4:18:01
 * @version: 1.0
 */
@RestController
@RequestMapping("/doctype/query")
public class QueryDoctypeService extends AbstractBaseQueryService<Doctype>{
	
	@Autowired
	private	ShiroSecurityService shiroSecurityService;
	
	@Autowired
	private	QueryDocService detailService;
	
	public QueryDoctypeService(){
		this.moduleName = ConstSysParam.MODULE_DOCTYPE;
		this.subSystem = ConstSysParam.SUBSYSTEM_DOCUMENT;
		this.tableName = ConstSysParam.MODULE_DOCTYPE;
		this.clazz = Doctype.class;
		
		typeMap = this.getTypeMap(clazz);
		
		orderMap.put("sortedid","desc");			
		
	}
	
	@PersistenceContext(unitName=ConstSysParam.SUBSYSTEM_SYSMAN)
	@Autowired
	protected void setEntityManager(EntityManager entityManager){
		this.entityManager = entityManager;		
	}
	
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return this.getBizMainPage();
	}
	
	/* (non-Javadoc)
	 * @see org.brisling.service.sysman.DepartmentServiceInf#getEditPage()
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return this.getBizEditPage();
	}
	
	@RequestMapping("/getListDomain.action")
	public ListDomain<Doctype> getListDomain(@RequestBody(required=false) String body){	
		
		return super.getListDomainByConditionAndOrder(body, typeMap, orderMap,true);
		
	}
	
	
	
	
	
	@RequestMapping("/getSubListDomain.action")
	public ListDomain<Doctype> getSubListDomain(@RequestParam(value="bizcode",required=false) String bizcode){	
		
		ListDomain<Doctype> domain = new ListDomain<Doctype>();
		
		if(bizcode!=null && bizcode.trim().length()>0){
			//判断分解typecode
			bizcode = "'" + bizcode +"'";
			//执行查询
			String sql = String.format("select * from %1$s where parentid in (select id from %1$s where bizcode in (%2$s)) order by sortedid", this.moduleName,bizcode);
			List<Doctype> lists=super.getAllList(sql);
			if(lists!=null && lists.size()>0){
				domain.setRows(lists);
				domain.setTotal(new Long(lists.size()));
			}
		}
		return domain;
		
	}
	
	
	@RequestMapping("/getTreeList.action")
	public List<Doctype> getTreeList(@RequestParam(value="id",required=false) Long id){
		
		return this.getPagedTreeList(id);
		
	}
	
	
	@RequestMapping("/getList.action")
	public List<Doctype> getList(
			@RequestParam(value="id",required=false) Integer id){		
		
		StringBuilder sb = new StringBuilder();
		String sql = "";
		String whereCorse=null;
		if(id!=null){
			whereCorse=String.format(sb.append("id = %1$d").toString(),id);
			
		}else{
			whereCorse="parentid =1";
		}
		sql = String.format(this.TWO_SQL, this.tableName,whereCorse,"sortedid");
		return super.getAllList(sql);
	}
	
	/**
	 * 获取部门管理的分类
	 * @param body
	 * @return
	 */
	@RequestMapping("/getDetpTypeListDomain.action")
	public List<Doctype> getDetpTypeListDomain(){	
		
		String sql = "";
		String whereCorse=null;
		
		String userid = shiroSecurityService.getCurrentUserIdentify();
		Long deptid =SysConfig.getUsr(userid).getDeptid();
		if(!this.isSuperDept(userid,ConstSysParam.QUERY_ALL, "query_doc_all")){	
			String _corse = "(parentid=1 and (owner_deptid is null or owner_deptid =%1$d)) or owner_deptid = %1$d ";
//			whereCorse=String.format("parentid =1 and (owner_deptid = %1$d or owner_deptid is null)",deptid);
			whereCorse=String.format(_corse,deptid);
		}else{
			whereCorse="parentid =1 and (is_open = '1' or is_open is null)";
		}
		sql = String.format(this.TWO_SQL, this.tableName,whereCorse,"sortedid");
		return super.getAllList(sql);
		
	}
	
	/**
	 * 根据用户id查找用户信息
	 * @param id	用户id
	 * @return 用户信息记录 
	 */
	@RequestMapping("/getOne.action")
	public Doctype getOne(@RequestParam(value="id",required=false) Long id){
		return super.getOne(id);
	}
	
	/**
	 * 根据用户唯一编码查找用户信息
	 * @param identify	用户唯一编码
	 * @return 用户信息记录
	 */
	@RequestMapping("/getOneByIdentify.action")
	public Doctype getOneByIdentify(@RequestParam(value="identify",required=false) String identify){	
		return super.getOneByIdentify(identify);
	}
	
	
	
	@RequestMapping("/getOneByField.action")
	public Doctype getOneByField(@RequestParam(value="field",required=false) String field,@RequestParam(value="fieldVal",required=false) String fieldVal){	
		return super.getOneByIdentify(field,fieldVal);
	}	
	
	/**
	 * 获取移动端首页显示类别
	 * @return
	 */
	public List<VODoctype> getTypeList(){
		String sql = "select * from %1$s where home_show = '1' order by home_sort";
		
		sql = String.format(sql, this.moduleName);
		List<VODoctype> lists = null;;
		try {
			lists = super.getClassedAllList(sql, VODoctype.class);
		} catch (DangerousOperatorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return lists;
	}
	
	/**
	 * 获取移动端数据
	 * @param body
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/mobile.m")
	public WSView<VODoctype> getMobile(@RequestBody(required=false) String body){
		
		Map<String,Object> map = JsonUtil.body2Class(body, Map.class);
		return exec(map);
	}	
	/**
	 * 移动端调用入口
	 * @param map
	 * @return
	 */
	public WSView<VODoctype> exec(Map<String,Object> map) {
		// TODO Auto-generated method stub
		
		WSView<VODoctype> ret = null;
		
		if(map!=null){
			String module = MapUtil.getMapData(map, "module");
			
			
			String method = MapUtil.getMapData(map, "method");
			if( (module!=null) && (method!=null) ){
				
				switch(method){
				case "getTypeList":
					//获取首页类型列表
					List<VODoctype> lists = this.getTypeList();					
					ret = new WSView<VODoctype>(module,method);
					if(lists!=null)
					ret.setRows(lists);	
					break;
				}
				
			}
		}
		return ret;
	}
	
}
